# 清理已经注册的

# 设置要遍历的目录路径
$directoryPath = "./Web"


# 获取目录中的所有项
$items = Get-ChildItem -Path $directoryPath

# 遍历每个项
foreach ($siteName in $items) {
    # 检查站点是否已经存在
    $site = Get-Website -Name $siteName -ErrorAction SilentlyContinue

    if ($site) {
        Write-Host "站点 $siteName 已存在，正在停止并删除..."
        # 停止站点
        Stop-WebSite -Name $siteName -ErrorAction SilentlyContinue
        # 删除站点
        Remove-WebSite -Name $siteName -ErrorAction SilentlyContinue
    } else {
        Write-Host "站点 $siteName 不存在，准备进行安装..."
    }
}

##########################


$absolutePath = Resolve-Path -Path "."


$Apps = "Static","Base","BaseAPI","Sett","SettAPI","CIS","CISAPI","PDS","PDSAPI","EMR","Herp","MRMS","MRQC","ApiManage","his.baseapi","Union"

# 注册 Static
New-Website -Name "Static" -PhysicalPath "$($absolutePath.Path+"\Static")" -Port 8014 -IPAddress "*"
# 启动站点
Start-WebSite -Name "Static"

# 注册Base
New-Website -Name "Base" -PhysicalPath "$($absolutePath.Path+"\Base")" -Port 8001 -IPAddress "*"
# 启动站点
Start-WebSite -Name "Base"

# BaseAPI
New-Website -Name "BaseAPI" -PhysicalPath "$($absolutePath.Path+"\BaeAPI")" -Port 7001 -IPAddress "*"
# 启动站点
Start-WebSite -Name "BaseAPI"

# Sett
New-Website -Name "Sett" -PhysicalPath "$($absolutePath.Path+"\Sett")" -Port 8002 -IPAddress "*"
# 启动站点
Start-WebSite -Name "Sett"

# SettAPI
New-Website -Name "SettAPI" -PhysicalPath "$($absolutePath.Path+"\SettAPI")" -Port 7002 -IPAddress "*"
# 启动站点
Start-WebSite -Name "SettAPI"

# CIS
New-Website -Name "CIS" -PhysicalPath "$($absolutePath.Path+"\CIS")" -Port 8003 -IPAddress "*"
# 启动站点
Start-WebSite -Name "CIS"

# CISAPI
New-Website -Name "CISAPI" -PhysicalPath "$($absolutePath.Path+"\CISAPI")" -Port 7003 -IPAddress "*"
# 启动站点
Start-WebSite -Name "CISAPI"

# PDS
New-Website -Name "PDS" -PhysicalPath "$($absolutePath.Path+"\PDS")" -Port 8004 -IPAddress "*"
# 启动站点
Start-WebSite -Name "PDS"

# PDSAPI
New-Website -Name "PDSAPI" -PhysicalPath "$($absolutePath.Path+"\PDSAPI")" -Port 7004 -IPAddress "*"
# 启动站点
Start-WebSite -Name "PDSAPI"

# EMR
New-Website -Name "EMR" -PhysicalPath "$($absolutePath.Path+"\EMR")" -Port 8005 -IPAddress "*"
# 启动站点
Start-WebSite -Name "EMR"

# OR
New-Website -Name "OR" -PhysicalPath "$($absolutePath.Path+"\OR")" -Port 8006 -IPAddress "*"
# 启动站点
Start-WebSite -Name "OR"

# ORAPI
New-Website -Name "ORAPI" -PhysicalPath "$($absolutePath.Path+"\ORAPI")" -Port 7006 -IPAddress "*"
# 启动站点
Start-WebSite -Name "ORAPI"

# Herp
New-Website -Name "Herp" -PhysicalPath "$($absolutePath.Path+"\Herp")" -Port 8007 -IPAddress "*"
# 启动站点
Start-WebSite -Name "Herp"

# MRMS
New-Website -Name "MRMS" -PhysicalPath "$($absolutePath.Path+"\MRMS")" -Port 8008 -IPAddress "*"
# 启动站点
Start-WebSite -Name "MRMS"

# MRQC
New-Website -Name "MRQC" -PhysicalPath "$($absolutePath.Path+"\MRQC")" -Port 8009 -IPAddress "*"
# 启动站点
Start-WebSite -Name "MRQC"

# 检查应用程序池是否存在，并创建如果不存在
function Check-And-CreateAppPool {
    param (
        [string]$AppPoolName,
        [string]$DotNetVersion = "v4.0",            # 可选参数，指定 .NET 版本
        [string]$PipelineMode = "Integrated"        # 可选参数，集成模式（Integrated）或经典模式（Classic）
    )

    # 检查应用程序池是否存在
    $appPool = Get-Item "IIS:\AppPools\$AppPoolName" -ErrorAction SilentlyContinue
    
    if (-Not $appPool) {
        # 创建应用程序池
        New-WebAppPool -Name $AppPoolName
        Write-Host "应用程序池 '$AppPoolName' 已成功创建。"

        # 设置 .NET 版本
        Set-ItemProperty "IIS:\AppPools\$AppPoolName" -Name managedRuntimeVersion -Value $DotNetVersion
        
        # 设置集成/经典模式
        Set-ItemProperty "IIS:\AppPools\$AppPoolName" -Name managedPipelineMode -Value $PipelineMode
        Write-Host "应用程序池 '$AppPoolName' 模式已设置为 '$PipelineMode'。"

        
        # 启动应用程序池
        Start-WebAppPool -Name $AppPoolName
        Write-Host "应用程序池 '$AppPoolName' 已启动并配置。"
    } else {
        Write-Host "应用程序池 '$AppPoolName' 已存在。"
    }
}

#AuthCenterAPI

# 检查应用程序池是否存在
Check-And-CreateAppPool -AppPoolName "AuthCenterAPI" -DotNetVersion "v4.0" -PipelineMode "Classic"
New-Website -Name "AuthCenterAPI" -PhysicalPath "$($absolutePath.Path+"\AuthCenterAPI")" -ApplicationPool "AuthCenterAPI" -Port 8015 -IPAddress "*"
# 启动站点
Start-WebSite -Name "AuthCenterAPI"

#ApiManage

# 检查应用程序池是否存在
Check-And-CreateAppPool -AppPoolName "ApiManage" -DotNetVersion "v4.0" -PipelineMode "Classic"
New-Website -Name "ApiManage" -PhysicalPath "$($absolutePath.Path+"\ApiManage")" -ApplicationPool "ApiManage" -Port 8016 -IPAddress "*"
# 启动站点
Start-WebSite -Name "ApiManage"


#Report

# 检查应用程序池是否存在
Check-And-CreateAppPool -AppPoolName "Report" -DotNetVersion "v4.0" -PipelineMode "Classic"
New-Website -Name "Report" -PhysicalPath "$($absolutePath.Path+"\Report")" -ApplicationPool "Report" -Port 8000 -IPAddress "*"
# 启动站点
Start-WebSite -Name "Report"

# his.baseapi
Check-And-CreateAppPool -AppPoolName "his.baseapi" -DotNetVersion "v4.0" -PipelineMode "Integrated"
New-Website -Name "his.baseapi" -PhysicalPath "$($absolutePath.Path+"\his.baseapi")" -ApplicationPool "his.baseapi" -Port 8017 -IPAddress "*"
# 启动站点
Start-WebSite -Name "his.baseapi"

# Union
Check-And-CreateAppPool -AppPoolName "Union" -DotNetVersion "v4.0" -PipelineMode "Integrated"
New-Website -Name "Union" -PhysicalPath "$($absolutePath.Path+"\Union")" -ApplicationPool "Union" -Port 8018 -IPAddress "*"
# 启动站点
Start-WebSite -Name "Union"